// gui e evt
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

// net
import java.io.*;
import java.net.*;

// list
import java.util.List;
import java.util.ArrayList;

public class Request {
	public static void main(String[] args) {

		// Criando a Janela
		JFrame window = new JFrame("Request");
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setSize(400, 300); // tamanho da janela

		window.setLayout(new BorderLayout());

		// Criando a area que tera botao
		JTextArea totalArea = new JTextArea();
		totalArea.setEditable(false);
		JScrollPane scrollPane = new JScrollPane(totalArea);
		window.add(scrollPane, BorderLayout.CENTER);

		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(4, 1));

		JTextField campUrl = new JTextField("https://google.com");
		panel.add(new JLabel("Digite o URL base (HOST): "));
		panel.add(campUrl);

		JButton selectArqButton = new JButton("Wordlist: ");
		panel.add(selectArqButton);

		JButton reqButton = new JButton("Requisitar");
		panel.add(reqButton);

		window.add(panel, BorderLayout.NORTH);

		selectArqButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				int returnValue = fileChooser.showOpenDialog(window);
				if (returnValue == JFileChooser.APPROVE_OPTION) {
					File file = fileChooser.getSelectedFile();
					try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
						List<String> listParams = new ArrayList<>();
						String line;
						while ((line = reader.readLine()) != null) {
							listParams.add(line);
						}
						String baseUrl = campUrl.getText().trim();
						StringBuilder resultBuilder = new StringBuilder();
						for (String param : listParams) {
							String urlReq = baseUrl + param;
							String reqResult = request(urlReq);
							resultBuilder.append(reqResult).append("\n");
						}
						totalArea.setText(resultBuilder.toString());
					} catch (IOException ex) {
						totalArea.setText("Error read to wordlist!!");
					}
				}
			}
		});

		reqButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String baseUrl = campUrl.getText().trim();
				String[] listParams = new String[] {"/imagens", "/maps", "/videos"};
				StringBuilder resultBuilder = new StringBuilder();
				for (String param : listParams) {
					String urlReq = baseUrl + param;
					String reqResult = request(urlReq);
					resultBuilder.append(reqResult).append("\n");
				}
				totalArea.setText(resultBuilder.toString());
			}
		});

		window.setVisible(true);
	}

	public static String request(String urlString) {
		String requestResult = "";
		try {
			URL urlRequest = new URL(urlString);
			HttpURLConnection conn = (HttpURLConnection) urlRequest.openConnection();
			conn.setRequestMethod("GET");
			int responseCode = conn.getResponseCode();
			requestResult = String.format("%s Code: %d", urlString, responseCode);
			conn.disconnect();
		} catch (Exception e){
			e.printStackTrace();
			requestResult = "error to access URL: " + urlString;
		}
		return requestResult;
	}
}